package org.apache.solr.formula;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.servlet.SolrServlet;
import org.apache.solr.servlet.SolrServletRequest;

import cn.ac.cintcm.prespring.tree.Ladder;
import cn.ac.cintcm.prespring.tree.VisitMemory;
import cn.ac.cintcm.prespring.tree.WebTree;
import cn.ac.cintcm.prespring.util.FileUtil;
import cn.ac.cintcm.prespring.util.IndexFile;
import cn.ac.cintcm.prespring.util.NameIndex;

public class TreeRequest extends SolrServlet {

    private static final long serialVersionUID = -6631457929819974974L;
    final Logger log = Logger.getLogger(TreeRequest.class.getName());
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        if( hasMulticore ) {
          response.sendError( 400, "Missing solr core name in path" );
          return;
        }
        
        final SolrCore core = SolrCore.getSolrCore();
        SolrResourceLoader resourceLoader = core.getResourceLoader();
        SolrServletRequest solrReq = new SolrServletRequest(core, request);
        SolrParams params = solrReq.getParams();
        List<String> values = new ArrayList<String>();
        values.add("cids");
        values.add("name");
        
        ElementsIndex ei = new ElementsIndex(solrReq.getSearcher(), "id", values);
        IndexFile idIndex = new IndexFile(ei.getIndex("cids"));
        
        String idstr = params.get("q");
        String[] ida = idstr.split(":");
        if (ida != null && ida.length == 2) {
            
            Ladder ld = new Ladder(idIndex);
            VisitMemory vm = ld.getLadder(ida[1]);
            //Set<Integer> idSets = vm.getLadderId();
            //Set<Integer> elementsSets = vm.getLadderElementId();
            NameIndex nameIndex = new NameIndex(ei.getIndexName("name"));
            String path = resourceLoader.getDataDir() + "/pre";
            
            List<String> elements = FileUtil.read(path, "herbsIndex.txt");
            NameIndex elementIndex = new NameIndex(elements);
            
            List<String> templateStrList = FileUtil.read(path, "template.txt");

            WebTree wt = new WebTree(elementIndex,nameIndex);
            
            wt.setIndex(Integer.valueOf(ida[1]), idIndex);
            wt.setTree(vm);
            wt.setLink("sim/?wt=xslt&tr=similar.xsl&q=id:");
            response.setContentType("text/html");
            String out = wt.createWebTree(templateStrList).toString();
            ServletOutputStream pw = response.getOutputStream();
            pw.write(out.getBytes("utf-8"));
        }
        solrReq.close();
      }
}
